﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using Microsoft.Office.Tools;
using System.Collections;
using LightingImportTool.Model;
using System.IO;
using Tools;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Xml;
using System.Web.Mail;
using LightingImportTool.Tools;
using LightingImportTool.Help;
using System.Reflection;

namespace LightingImportTool
{
    public class ImageFile
    {
        public string Name { get; set; }
        public byte[] File { get; set; }
    }

    public class ImageModel : ICloneable
    {
        public string Key { get; set; }
        public string Name { get; set; }
        public double Width { get; set; }
        public double Height { get; set; }
        public long Length { get; set; }
        public string Format { get; set; }

        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }

    public class WorkbookModel
    {
        public string Key { get; set; }

        private List<ImageFile> _imageFiles;
        private List<ImageModel> _imageModels;

        public List<ImageFile> ImageFiles
        {
            get
            {
                if (_imageFiles == null)
                {
                    _imageFiles = new List<ImageFile>();
                }
                return _imageFiles;
            }
        }

        public List<ImageModel> ImageModels
        {
            get
            {
                if (_imageModels == null)
                {
                    _imageModels = new List<ImageModel>();
                }
                return _imageModels;
            }
        }

        public ImageFile GetImageFile(string name)
        {
            return (from r in ImageFiles
                    where r.Name == name
                    select r).FirstOrDefault();
        }

        public ImageModel GetImageModel(string key)
        {
            return (from r in ImageModels
                    where r.Key == key
                    select r).FirstOrDefault();
        }

        public ImageModel GetImageModelByName(string name)
        {
            return (from r in ImageModels
                    where r.Name == name
                    select r).FirstOrDefault();
        }
    }

    public partial class ThisAddIn
    {

        private Dictionary<Excel.Workbook, WorkbookModel> _workbookModels = new Dictionary<Excel.Workbook, WorkbookModel>();

        public Dictionary<Excel.Workbook, WorkbookModel> WorkbookModels
        {
            get
            {
                return _workbookModels;
            }
        }

        public WorkbookModel GetActiveWorkbookModel()
        {
            Excel.Workbook workbook = this.Application.ActiveWorkbook;
            if (_workbookModels.ContainsKey(workbook))
            {
                return _workbookModels[workbook];
            }

            WorkbookModel model = new WorkbookModel();
            _workbookModels[workbook] = model;
            return model;
        }

        public Excel.Worksheet GetActiveWorkSheet()
        {
            return (Excel.Worksheet)this.Application.ActiveSheet;
        }

        public Excel.Range GetActiveRange()
        {
            return (Excel.Range)this.Application.Selection;
        }

        public string GetRangeKey()
        {
            Excel.Worksheet worksheet = GetActiveWorkSheet();

            
            Excel.Range range = null;
            try
            {
                range = GetActiveRange();
                return string.Format("{0}|{1}|{2}", worksheet.Name, range.Row, range.Column);
            }
            catch(Exception ex)
            {
            }
            
            if(range == null)
            {
                try
                {
                   dynamic obj = Globals.ThisAddIn.Application.Selection.GetType().InvokeMember(
                        "ShapeRange", 
                        BindingFlags.Default | BindingFlags.GetProperty, 
                        null,
                        Globals.ThisAddIn.Application.Selection, 
                        null);

                    return string.Format("{0}|{1}|{2}", worksheet.Name, obj.TopLeftCell.Row, obj.TopLeftCell.Column);
                }
                catch(Exception ex)
                {
                }
            }
            return null;
        }


        public string GetNowTimeString()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmssffff");
        }


        private Timer _timer;

        private Timer Timer
        {
            get
            {
                if (_timer == null)
                {
                    _timer = new Timer();
                    _timer.Interval = 50000;
                    _timer.Tick += _timer_Tick;
                }
                return _timer;
            }
        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            Timer.Stop();
            if (Timer.Tag != null)
            {
                string fileName = Timer.Tag.ToString();
                if (File.Exists(fileName))
                {
                    try
                    {
                        File.Delete(fileName);
                    }
                    catch (Exception ex)
                    {
                        ExceptionWindow window = new ExceptionWindow(ex);
                        window.ShowDialog();
                    }
                }
            }
        }

        
        


            

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
        }

        private void Application_SheetSelectionChange(object Sh, Excel.Range Target)
        {
        }


        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}
